<!--
 * @Description: 商品搜索
 * @Author: hmt
 * @Date: 2024-03-27 10:41:31
 * @LastEditors: hmt
 * @LastEditTime: 2024-04-02 07:37:25
-->
<template>
  <div class="search">
    <div class="page-small">
      <div class="search-box">
        <div class="input">
          <input v-model.trim="keyword"
                 @keydown.enter="onSearch"
                 placeholder="请输入关键字回车搜索" />
        </div>
        <p class="btn"
           @click="onSearch">搜索</p>
      </div>
    </div>
    <div v-loading="loading"
         class="search-content big-scrollbar">
      <base-empty v-if="!list.length" />
      <div class="page-small">
        <ul class="good-wrap">
          <li v-for="item in list"
              :key="item.id"
              class="good-item">
            <p class="g-img">
              <img :src="item.imgUrl"
                   width="100%"
                   height="100%" />
            </p>
            <p class="g-price">
              <span class="symbol">￥</span>{{item.price}}
            </p>
            <div class="g-name">
              <base-ellipsis-tip :content="item.content"
                                 :line="2" />
            </div>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, onMounted, ref } from 'vue'
import BaseEmpty from '@/components/base-empty/index.vue'
import { searchGoods } from '@/api/goods'

const hightLightWord = (content: string, keyword: string) => {
  const _reg = new RegExp(keyword, 'g')
  const newContent = content.replace(_reg, `<span class="cell-red">${keyword}</span>`)
  return newContent
}

export default defineComponent({
  name: 'search',
  components: {
    BaseEmpty
  },
  setup() {
    const keyword = ref('')
    const list = ref([])
    const loading = ref(false)

    const onSearch = async () => {
      if (loading.value) return false
      loading.value = true
      const data = await searchGoods(keyword.value)
      loading.value = false
      list.value = (data || []).map(item => {
        item.content = item.name + ' ' + item.desc
        if (keyword.value) item.content = hightLightWord(item.content, keyword.value)
        return item
      })
    }

    onMounted(() => {
      onSearch()
    })

    return {
      keyword,
      list,
      loading,
      onSearch
    }
  }
})
</script>

<style lang="scss" scoped>
.search {
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  .search-box {
    @extend %flexHorizontal;
    width: 705px;
    border: 2px solid $red;
    margin: 30px auto 25px;
    .input {
      flex: 1;
    }
    input {
      width: 100%;
      height: 34px;
      padding: 5px 10px;
    }
    .btn {
      width: 80px;
      line-height: 34px;
      text-align: center;
      background-color: $red;
      color: $white;
      font-size: $font-size-middle;
      cursor: pointer;
    }
  }
  .search-content {
    flex: 1;
    padding-bottom: 22px;
    overflow-y: auto;
    border-top: 2px solid $red;
  }
  .good-wrap {
    display: flex;
    flex-wrap: wrap;
  }
  .good-item {
    font-size: $font-size-sm;
    width: 240px;
    padding: 12px 10px;
    margin-top: 10px;
    border: 1px solid $white;
    transition: border-color 0.1s ease;
    cursor: pointer;
    &:hover {
      border-color: $border-color;
      box-shadow: 0 0 2px 2px #f8f8f8;
    }
    .g-img {
      display: block;
      height: 220px;
      margin-bottom: 5px;
    }
    .g-price {
      color: $red;
      font-size: $font-size-title;
      margin-bottom: 8px;
      .symbol {
        font-size: $font-size-middle;
      }
    }
    .g-name {
      color: $gray-text-color;
      height: 36px;
      overflow: hidden;
    }
  }
}
</style>
